﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace asMeishi.web.Models
{
    using Classes;
    public static class entityClassEx
    {
        #region item 菜 实体类 的扩展


        public static string getHeadPic(this food item,Boolean big=false)
        {
            string headPic;
            if (item.picUrls != null && item.picUrls.Count() != 0)
            {
                if (big != true)
                {
                    headPic = "/upload/foodPic/s/" + item.picUrls.First() + ".jpg";
                }
                else
                {
                    headPic = "/upload/foodPic/" + item.picUrls.First();
                }

            }
            else
            {
                headPic = baseConfig.siteThemePath + "images/shopNullPic.png";
            }
            return headPic;

        }
        public static string getShopName(this food item)
        {
            if (item.shopId==null) return "未知";
            var db =new  DataSession<MongoDataContext, shop>();
            
                var data = db.GetById( item.shopId);
                if (data == null) return "未找到";
                return data.name;
            
        }

        public static string getUserName(this food item)
        {
            historyC<food> foodHistory = new historyC<food>(item);
            var createHistory = foodHistory.get().FirstOrDefault();

            if (createHistory != null)
            {
                if (createHistory.userId == null) return "未找到";

                var data = new DataSession<MongoDataContext, user>().GetById(createHistory.userId);
                if (data.Id == null) return "未找到";
                return data.name;
            }
            else {
                return "未知";
            }
        }
        public static string getLastUserName(this food item)
        {
            historyC<food> foodHistory = new historyC<food>(item);
            var createHistory = foodHistory.get().LastOrDefault();

            if (createHistory != null)
            {
                if (createHistory.userId == null) return "未找到";

                var data = new DataSession<MongoDataContext, user>().GetById(createHistory.userId);
                if (data.Id == null) return "未找到";
                return data.name;
            }
            else
            {
                return "未知";
            }
        }
        /*
        public static List<asDigit.Models.entityExpClass.impress> getImpression(this item item)
        {
            using (var db = new MongoSession<asDigit.Models.entityExpClass.item>())
            {
                var data = db.findOne(new { Id = item.Id });
                if (data == null) return new List<asDigit.Models.entityExpClass.impress>();
                return data.Impression ?? new List<asDigit.Models.entityExpClass.impress>();
            }
        }

        public static List<asDigit.Models.entityExpClass.impress> addImpression(this item item, string content)
        {
            using (var db = new MongoSession<asDigit.Models.entityExpClass.item>())
            {
                var data = db.findOne(new { Id = item.Id });
                if (data == null) new asDigitException("item的实例错误");


                asDigit.Models.entityExpClass.impress itemImpression = new asDigit.Models.entityExpClass.impress();
                itemImpression.content = content;
                itemImpression.datetime = DateTime.Now;
                itemImpression.userId = userC.loginUser.Id;
                if (data.Impression == null || data.Impression.Count==0)
                {
                    data.Impression = new List<asDigit.Models.entityExpClass.impress> { itemImpression };
                }
                else
                {
                    data.Impression.Add(itemImpression);
                }
                db.Save(data);
                return data.Impression;
            }
        }

        public static List<asDigit.Models.entityExpClass.impress> addImpression(this item item)
        {
            using (var db = new MongoSession<asDigit.Models.entityExpClass.item>())
            {
                var data = db.findOne(new { Id = item.Id });
                if (data == null) new asDigitException("item的实例错误");
                if (data.Impression == null || data.Impression.Count == 0)
                {
                    return new List<asDigit.Models.entityExpClass.impress>();
                }
                else
                {
                    return data.Impression;
                }
            }
        }

        public static void removeImpression(this item item, string content)
        {
            using (var db = new MongoSession<asDigit.Models.entityExpClass.item>())
            {
                var data = db.findOne(new { Id = item.Id });
                if (data == null) new asDigitException("item的实例错误");
                if (data.Impression == null || data.Impression.Count == 0) return;
                var temp = data.Impression.FirstOrDefault(d => d.content == content);
                if (temp == null) return;
                data.Impression.Remove(temp);
                db.Save(data);
            }
        }
        */
        #endregion

        #region shop 店 实体类 的扩展
        public static string getHeadPic(this shop item,Boolean big=false)
        {
            string headPic;
            if (item.picUrlList != null && item.picUrlList.Count()!=0)
            {
                if (big != true)
                {
                    headPic = "/upload/shopPic/s/" + item.picUrlList.First() + ".jpg";
                }
                else
                {
                    headPic = "/upload/shopPic/" + item.picUrlList.First();
                }
                
            }
            else
            {
                headPic = baseConfig.siteThemePath + "images/shopNullPic.png";
            }
            return headPic;

        }
        public static void addimpression()
        {


        }

        #endregion

        #region comment 实体类 的扩展
        
        public static string shopName(this comment comment)
        {
            if (comment.targetId == null) return "未知";
            var db = new DataSession<MongoDataContext, shop>();

            var data = db.GetById(comment.targetId);
            if (data == null) return "未找到";
            return data.name;

        }
        public static string foodName(this impression imp) {
            food data =null;
            if (imp.targetId == null) return "未知";

            if (imp.targetType == typeof(food).Name)
            {
                var db = new DataSession<MongoDataContext, food>();
                data = db.GetById(imp.targetId);
            }


            if (data == null) return "未找到";
            return data.name;        
        }

        public static string shopName(this impression imp)
        {
            dynamic data;
            if (imp.targetId == null) return "未知";
            if (imp.targetType == typeof(shop).Name)
            {
                var db = new DataSession<MongoDataContext, shop>();
                data = db.GetById(imp.targetId);
            }
            else {
                var db = new DataSession<MongoDataContext,food>();

                food f = db.GetById(imp.targetId);
                if (f == null) return "未找到";
                return f.getShopName();
            }

            
            if (data == null) return "未找到";
            return data.name;

        }
        //已有。在comment数据创建时写入当时的userName;
        public static string getUserName(this comment comment)
        {
            historyC<comment> foodHistory = new historyC<comment>(comment);
            var createHistory = foodHistory.get().FirstOrDefault();

            if (createHistory != null)
            {
                if (createHistory.userId == null) return "未找到";

                var data = new DataSession<MongoDataContext, user>().GetById(createHistory.userId);
                if (data.Id == null) return "未找到";
                return data.name;
            }
            else
            {
                return "未知";
            }
        }
        
        #endregion
       

    }

}
